import 'package:flutter/material.dart';
import 'package:jd_flutter/base/widget/basic/base_gesture.dart';
import 'package:jd_flutter/base/widget/image/jd_asset_image.dart';

typedef TDCheckBoxCallback = void Function(bool isSelected);

class CheckboxButton extends StatefulWidget {
  TDCheckBoxCallback? valueChanged;
  final bool selected;
  final double? size;
  final EdgeInsets? padding;
  final String? selectedPicture;
  final String? unSelectedPicture;

  CheckboxButton(
      {super.key,
      this.selected = false,
      this.valueChanged,
      this.size,
      this.padding,
      this.selectedPicture,
      this.unSelectedPicture});

  @override
  State<CheckboxButton> createState() => _CheckboxButtonState();
}

class _CheckboxButtonState extends State<CheckboxButton> {
  late final ValueNotifier<bool> valueNotifier;
  @override
  void initState() {
    valueNotifier = ValueNotifier(widget.selected);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return CommonGestureDetector(
      onTap: () {
        valueNotifier.value = !valueNotifier.value;
        widget.valueChanged?.call(valueNotifier.value);
      },
      child: Padding(
        padding: widget.padding ?? EdgeInsets.zero,
        child: ValueListenableBuilder(
          valueListenable: valueNotifier,
          builder: (BuildContext context, bool value, Widget? child) {
            return Visibility(
              visible: !value,
              replacement: JDAssetImage(
                widget.selectedPicture ?? '$fLoginFolder/login_checked.png',
                width: widget.size ?? 15,
                height: widget.size ?? 15,
              ),
              child: JDAssetImage(
                widget.unSelectedPicture ?? '$fLoginFolder/login_uncheck.png',
                width: widget.size ?? 15,
                height: widget.size ?? 15,
              ),
            );
          },
        ),
      ),
    );
  }
}
